<?php

/**
 *   员工角色管理
 */

namespace app\admin\controller;

use app\facade\Basic;
use think\Request;
use app\admin\service\Role as ServiceRole;

class Role extends Base
{
    protected $request;
    protected $service;
    protected $validate;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\Role();
        $this->validate = new \app\admin\validate\Department();
    }


    /**
     *  角色列表
     */
    public function index(Request $request)
    {


        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $pageIndex  = isset($params['pageIndex'])    ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])     ? intval($params['pageSize']) : 10;
        $pid        = isset($params['pid'])          ? intval($params['pid']) : null;
        $name       = isset($params['name'])         ? trim($params['name']) : null;
        $createTime = isset($params['create_time'])  ? trim($params['create_time']) : null;
        $updateTime = isset($params['update_time'])  ? trim($params['update_time']) : null;


        $where = ['is_del' => 0];
        if ($pid > 0) {
            $where['pid'] = $pid;
        }
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($createTime) {
            $where['create_time'] = $createTime;
        }
        if ($updateTime) {
            $where['update_time'] = $updateTime;
        }


        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     *  校色详情
     */
    public function detail(Request $request)
    {
        $param['id'] = $request->param('id', null);
        /* 验证 */
        //$this->validateData($param, 'Role.detail');
        $ServiceRole = new ServiceRole();
        $result = $ServiceRole->detail($param);
        return Basic::json($result);
    }


    /**
     *  创建角色
     */
    public function create(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['name'] = $request->param('name', null);
        $param['status'] = $request->param('status', null);
        $param['remark'] = $request->param('remark', null);
        $param['auth_ids'] = $request->param('auth_ids', null);
        $param['order_range'] = $request->param('order_range', null);

        /* 验证 */
        //$this->validateData($param, 'Role.setAuth');

        $ServiceRole = new ServiceRole();
        $result = $ServiceRole->create($param);
        return Basic::json($result);
    }


    /**
     *  设置角色于权限关联
     */
    public function update(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['name'] = $request->param('name', null);
        $param['status'] = $request->param('status', null);
        $param['remark'] = $request->param('remark', null);
        $param['role_id'] = $request->param('role_id', null);
        $param['auth_ids'] = $request->param('auth_ids', null);
        $param['order_range'] = $request->param('order_range', null);

        /* 验证 */
        $this->validateData($param, 'Role.setAuth');

        $ServiceRole = new ServiceRole();
        $result = $ServiceRole->update($param);
        return Basic::json($result);
    }


    /**
     *  删除角色
     */
    public function delete(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['id'] = $request->param('id', null);

        /* 验证 */
        $this->validateData($param, 'Role.remove');

        $Operator = new ServiceRole();
        $result = $Operator->delete($param);
        return Basic::json($result);
    }

    public function status(Request $request)
    {
        $param['operator_id'] = $request->operator_id;
        $param['ids'] = $request->param('ids', null);
        $param['status'] = $request->param('status', null);


        /* 验证 */
        $this->validateData($param, 'Role.status');

        $Operator = new ServiceRole();
        $result = $Operator->status($param);
        return Basic::json($result);
    }

    /**
     *  删除角色
     */
    public function del(Request $request)
    {
        $param['id'] = $request->param('id', null);

        /* 验证 */
        $this->validateData($param, 'Role.del');

        $Operator = new ServiceRole();
        $result = $Operator->del($param);
        return Basic::json($result);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-07-16 14:04:04 
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum()
    {
        $param['field'] = $this->request->param('field', null);
        $ServiceRole = new ServiceRole();
        $result = $ServiceRole->getFieldEnum($param);
        return Basic::json($result);
    }
}
